package com.zhengqing.system.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhengqing.common.base.constant.ServiceConstant;
import com.zhengqing.common.core.api.BaseController;
import com.zhengqing.system.config.MinIoTsProperties;
import com.zhengqing.system.entity.ManualCatalogOm;
import com.zhengqing.system.model.dto.ManualModelSearchDto;
import com.zhengqing.system.model.vo.ManualModelSearchVo;
import com.zhengqing.system.service.IManualCatalogOmService;
import com.zhengqing.system.service.IManualModelOmService;
import com.zhengqing.system.util.MinIoUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.List;

/**
 * <p>
 * 基础模块 - 数据字典接口
 * </p>
 *
 * @author des1gn
 * @description
 * @date 2020/4/15 20:50
 */
@Slf4j
@RestController
@RequestMapping(ServiceConstant.SERVICE_API_PREFIX_WEB_SYSTEM + "/manual")
public class ManualCatalogController extends BaseController {

    @Resource
    private IManualModelOmService manualModelOmService;

    @Resource
    private IManualCatalogOmService manualCatalogOmService;

    @Resource
    private MinIoTsProperties minIoTsProperties;

    @GetMapping("search")
    public IPage<ManualModelSearchVo> search(ManualModelSearchDto searchDto) {
        return this.manualModelOmService.searchModel(searchDto);
    }

    @GetMapping("getCataloUrl")
    public String getCataloUrl(Long modelId){
        ManualCatalogOm res = manualCatalogOmService.getOne(Wrappers.<ManualCatalogOm>lambdaQuery().eq(ManualCatalogOm::getModelId, modelId));
        if (StringUtils.isBlank(res.getCatalogMinioPath())){
            return Strings.EMPTY;
        }
        try {
            String url = MinIoUtil.getObjectUrl("manuals",res.getCatalogMinioPath(),60);
            return url.split("9000")[1];
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Strings.EMPTY;
    }

}
